<?php
//等级实体类
class LevelModel extends Model {
    public function __construct() {
        parent::__construct();
        $this->fields = array('id','level_name','level_info');
        $this->tables = array(DB_FREFIX.'level');
        $this->check = new LevelCheck();
    }
    /**
     * 取出所有的数据记录(有分页)
     *
     * @access public
     * @return array
     */
    public function findAll() {
        return parent::select(array('id','level_name','level_info'),
                              array('limit'=>$this->limit,
                                    'order'=>'id ASC'));
    }
    /**
     * 取出所有的数据记录(无分页)
     *
     * @access public
     * @return array
     */
    public function findAllNoPage(){
        return parent::select(array('id','level_name','level_info'),
                              array('order'=>'id ASC'));
    }
    /**
     * 取出一条数据
     *
     * @access public
     * @return array
     */
    public function findOne() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        return parent::select(array('id','level_name','level_info'),
                              array('where'=>$where,
                                    'limit'=>'1'));
    }
    /**
    * 添加管理员等级时判断是否已存在
    *
    * @access public
    * @return string
    */
    public function isOneByAjax() {
        $levelName = $this->input->get('level_name');
        $where = array("level_name='$levelName'");
        return parent::isOne($where);
    }
    /**
     * 添加管理员等级
     *
     * @access public
     * @return string 
     */
    public function add() {
        $addData = $this->input->post();
        if(!$this->check->addCheck($this,$addData)) die();
        $addData = $this->input->filter($addData,$this->fields);
        //var_dump($addData);die();
        return parent::add($addData);
    }
    /**
    * 删除管理员等级
    *
    * @access public
    * @return string
    */
    public function del() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        return parent::del($where);
    }
    /**
     * 更新管理员等级
     * @access public
     * @return string
     */
    public function update() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        $updateData = $this->input->post();
        if(!$this->check->updateCheck($updateData)) die();
        $updateData = $this->input->filter($updateData,$this->fields);
        return parent::update($where,$updateData);
    }
    /**
     * 总的等级记录数
     * @access public 
     * @return string
     */
    public function total() {
        return parent::total();
    }
}